package com.regedit.aplicacion.activities;

import java.io.File;
import java.io.FileOutputStream;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.regedit.aplicacion.R;

public class MainActivity extends Activity {

	Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.activity_main);
		Button boton = (Button) findViewById(R.id.btnLogin);

		/*
		 * AppPrefences preferences = new AppPrefences(this); String username =
		 * preferences.getUserName();
		 * 
		 * preferences.setUserName("hola");
		 */

		final SharedPreferences settings = mContext.getSharedPreferences(
				"android", Context.MODE_PRIVATE);
		String usuario = settings.getString("username", null);
		if (usuario != null) {
			EditText textUsuario = (EditText) findViewById(R.id.txtUsuario);
			textUsuario.setText(usuario);
		}

		boton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				EditText textUsuario = (EditText) findViewById(R.id.txtUsuario);
				String usuario = textUsuario.getText().toString();

				final SharedPreferences.Editor editor = settings.edit();
				editor.putString("username", usuario);
				editor.commit();

				Intent intent = new Intent(mContext, BienvenidoActivity.class);
				intent.putExtra("nombre", usuario);
				startActivity(intent);
			}
		});

		Button btnMapa = (Button) findViewById(R.id.btnMapa);
		btnMapa.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(mContext, MiMapaActivity.class);
				startActivity(intent);

			}
		});

		Button btnSpinner = (Button) findViewById(R.id.btnSpinner);
		btnSpinner.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(mContext, SpinnerActivity.class);
				startActivity(intent);

			}
		});

		Button btnWebView = (Button) findViewById(R.id.btnWebView);
		btnWebView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(mContext, WebViewActivity.class);
				startActivity(intent);

			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	public File getAlbumStorageDir(String albumName) {
		// Get the directory for the user's public pictures directory.
		File file = new File(
				Environment
						.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
				albumName);
		if (!file.mkdirs()) {
			Log.e("error", "Directory not created");
		}
		return file;
	}

	public void miMetodo(Context context) {
		String filename = "myfile";
		File file = new File(context.getFilesDir(), filename);

		String string = "Hello world!";
		FileOutputStream outputStream;

		try {
			outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
			outputStream.write(string.getBytes());
			outputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void makeJsonFromString() {
		String jsonString = "{\"nombre\":\"Pablo\",\"apellido\":\"Johnson\"}";
		try {
			JSONObject json = new JSONObject(jsonString);
			String nombre = json.getString("nombre");
			Log.e("nuevoJson", nombre);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void makeJson() {
		JSONObject json = new JSONObject();
		try {
			json.put("nombre", "Pablo");
			json.put("apellido", "Johnson");

			JSONArray jArray = new JSONArray();

			JSONObject carro1 = new JSONObject();
			carro1.put("marca", "Toyota");
			carro1.put("precio", "2000");

			JSONObject carro2 = new JSONObject();
			carro2.put("marca", "Nissan");
			carro2.put("precio", "5000");

			jArray.put(carro1);
			jArray.put(carro2);

			json.put("carros", jArray);

			Log.e("json", json.toString());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
